﻿#region Imports

using System;
using bGallery.Core.Common;
using bGallery.Core.Data;
using bGallery.Core.Domain;
using Spring.DataBinding;

#endregion

namespace bGallery.Web
{
	/// <summary>
	/// Album page
	/// </summary>
	public partial class Album : Page
	{
		#region Private fields

		private Core.Domain.Album selectedAlbum;
		private IAlbumDao albumDao;

		#endregion

		#region Public properties

		/// <summary>
		/// Gets selected album
		/// </summary>
		public Core.Domain.Album SelectedAlbum
		{
			get { return selectedAlbum; }
		}

		/// <summary>
		/// Sets album DAO
		/// </summary>
		public IAlbumDao AlbumDao
		{
			set { albumDao = value; }
		}

		#endregion

		#region Event handlers

		/// <summary>
		/// Handles on load event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Page_Load(object sender, EventArgs e)
		{
			InitAlbum();
		}

		/// <summary>
		/// Redirects back to gallery
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void BackToGallery(object sender, EventArgs e)
		{
			SetResult("Gallery");
		}

		#endregion

		#region Private methods

		/// <summary>
		/// Initializes selected album
		/// </summary>
		private void InitAlbum()
		{
			if(Request.Params["album"] != null)
			{
				if (Request.Params["album"] != null)
					selectedAlbum = albumDao.FindByAbbreviation(Request.Params["album"], SignedInUser);
			}

			if (selectedAlbum == null)
			{
				SetResult("GalleryNoCategory");
			}
			else
			{
				lblHeader.Text = selectedAlbum.Name;
				lblDescription.Text = selectedAlbum.Description;
				repPhotos.DataSource = selectedAlbum.Photos;
				repPhotos.DataBind();
			}
		}

		#endregion
	}
}
